package com.hyy.provider.service.impl;

import com.hyy.common.core.utils.MapstructUtils;
import com.hyy.common.core.utils.StringUtils;
import com.hyy.common.mybatis.core.page.TableDataInfo;
import com.hyy.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hyy.api.domain.provider.bo.ProvProviderChannelBo;
import com.hyy.api.domain.provider.vo.ProvProviderChannelVo;
import com.hyy.api.domain.provider.ProvProviderChannel;
import com.hyy.provider.mapper.ProvProviderChannelMapper;
import com.hyy.provider.service.IProvProviderChannelService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 供应商通道Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-20
 */
@RequiredArgsConstructor
@Service
public class ProvProviderChannelServiceImpl implements IProvProviderChannelService {

    private final ProvProviderChannelMapper baseMapper;

    /**
     * 查询供应商通道
     *
     * @param id 主键
     * @return 供应商通道
     */
    @Override
    public ProvProviderChannelVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询供应商通道列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 供应商通道分页列表
     */
    @Override
    public TableDataInfo<ProvProviderChannelVo> queryPageList(ProvProviderChannelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProvProviderChannel> lqw = buildQueryWrapper(bo);
        Page<ProvProviderChannelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的供应商通道列表
     *
     * @param bo 查询条件
     * @return 供应商通道列表
     */
    @Override
    public List<ProvProviderChannelVo> queryList(ProvProviderChannelBo bo) {
        LambdaQueryWrapper<ProvProviderChannel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ProvProviderChannel> buildQueryWrapper(ProvProviderChannelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ProvProviderChannel> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ProvProviderChannel::getId);
        lqw.eq(bo.getProviderId() != null, ProvProviderChannel::getProviderId, bo.getProviderId());
        lqw.like(StringUtils.isNotBlank(bo.getChannelName()), ProvProviderChannel::getChannelName, bo.getChannelName());
        lqw.eq(StringUtils.isNotBlank(bo.getProvinceCode()), ProvProviderChannel::getProvinceCode, bo.getProvinceCode());
        lqw.eq(StringUtils.isNotBlank(bo.getIspCode()), ProvProviderChannel::getIspCode, bo.getIspCode());
        lqw.eq(bo.getPrice() != null, ProvProviderChannel::getPrice, bo.getPrice());
        lqw.eq(bo.getStatus() != null, ProvProviderChannel::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getSensitiveWord()), ProvProviderChannel::getSensitiveWord, bo.getSensitiveWord());
        lqw.like(StringUtils.isNotBlank(bo.getHideProvinceName()), ProvProviderChannel::getHideProvinceName, bo.getHideProvinceName());
        lqw.eq(bo.getExtCodeLenLimit() != null, ProvProviderChannel::getExtCodeLenLimit, bo.getExtCodeLenLimit());
        return lqw;
    }

    /**
     * 新增供应商通道
     *
     * @param bo 供应商通道
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ProvProviderChannelBo bo) {
        ProvProviderChannel add = MapstructUtils.convert(bo, ProvProviderChannel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改供应商通道
     *
     * @param bo 供应商通道
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ProvProviderChannelBo bo) {
        ProvProviderChannel update = MapstructUtils.convert(bo, ProvProviderChannel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ProvProviderChannel entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除供应商通道信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
